package com.ruoyi.system.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.Likes;
import com.ruoyi.system.mapper.LikesMapper;
import com.ruoyi.system.service.ILikesService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 用户点赞Controller
 *
 * @author AousVolad
 * @date 2024-07-05
 */
@RestController
@RequestMapping("/likes/likes")
public class LikesController extends BaseController {
    @Autowired
    private ILikesService likesService;

    @Autowired
    private LikesMapper likesMapper;

    /**
     * 查询用户点赞列表
     */
    @PreAuthorize("@ss.hasPermi('likes:likes:list')")
    @GetMapping("/list")
    public TableDataInfo list(Likes likes) {
        startPage();
        List<Likes> list = likesService.selectLikesList(likes);
        return getDataTable(list);
    }


    /**
     * 导出用户点赞列表
     */
    @PreAuthorize("@ss.hasPermi('likes:likes:export')")
    @Log(title = "用户点赞", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Likes likes) {
        List<Likes> list = likesService.selectLikesList(likes);
        ExcelUtil<Likes> util = new ExcelUtil<Likes>(Likes.class);
        util.exportExcel(response, list, "用户点赞数据");
    }

    /**
     * 获取用户点赞详细信息
     */
    @PreAuthorize("@ss.hasPermi('likes:likes:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(likesService.selectLikesById(id));
    }

    /**
     * 新增用户点赞
     */
    @PreAuthorize("@ss.hasPermi('likes:likes:add')")
    @Log(title = "用户点赞", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Likes likes) {
        System.err.println(likes);
        return toAjax(likesService.insertLikes(likes));
    }

    /**
     * 查询 收藏 记录
     */
    @PreAuthorize("@ss.hasPermi('collect:collect:list')")
    @GetMapping("/one")
    public AjaxResult getNewsCollect(
            @Param("userId") Long userId,
            @Param("articleId") Long articleId,
            @Param("videosId") Long videosId) {
        return success(likesService.getOne(userId, articleId, videosId));
    }

    /**
     * 修改用户点赞
     */

    @PreAuthorize("@ss.hasPermi('likes:likes:edit')")
    @Log(title = "用户点赞", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Likes likes) {
        return toAjax(likesService.updateLikes(likes));
    }

    /**
     * 删除用户点赞
     */
    @PreAuthorize("@ss.hasPermi('likes:likes:remove')")
    @Log(title = "用户点赞", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(likesService.deleteLikesByIds(ids));
    }
}
